package com.lagou.dao.impl;

import com.lagou.dao.IUserDao;
import com.lagou.pojo.User;
import top.lwaiting.io.Resources;
import top.lwaiting.sqlSession.SqlSession;
import top.lwaiting.sqlSession.SqlSessionFactory;
import top.lwaiting.sqlSession.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

/**
 * @author YANG
 * @version V1.0
 * @date 2024/1/23 15:55
 * @see UserDaoImpl
 **/
public class UserDaoImpl implements IUserDao {
    @Override
    public List<User> findAll() throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().builder(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 调用
        List<User> results = sqlSession.selectList("user.selectList");
        return results;
    }

    @Override
    public User findByCondition(User user) throws Exception {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().builder(resourceAsStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();

        // 调用
        User result = sqlSession.selectOne("user.selectOne", user);
        return result;
    }
}
